package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ptr.ptrsystem.logica.*;
import com.ptr.controladores.*;
import java.sql.*;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "MantenimientoMatrimonioServlet", urlPatterns = {"/MantenimientoMatrimonio"})
public class MantenimientoMatrimonioServlet extends HttpServlet {

    public MantenimientoMatrimonioServlet() {
    }
    String NRO_ACTA_PER;
    String NRO_ACTA_MAT;
    String FECH_REG_MAT;
    String IDUBIGEO_MAT;
    String DNI_PER_1;
    String DNI_PER_2;
    String DNI_TEST_1;
    String DNI_TEST_2;
    String DNI_REGISTRADOR;
    String COD_ESTADO;
    String IDUBIGEO;
    String COD_NACIONALIDAD;
    String RUTA_ACTA_MAT;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

//        if((NRO_ACTA_PER!="")&&(NRO_ACTA_MAT!="")&&(FECH_REG_MAT!="")&&(IDUBIGEO_MAT!="")&&(DNI_PER_1!="")&&
//        (DNI_PER_2!="")&&(DNI_TEST_1!="")&&(DNI_TEST_2!="")&&(DNI_REGISTRADOR!="")&&(COD_ESTADO!="")&&
//        (IDUBIGEO!="")&&(COD_NACIONALIDAD!="")&&(RUTA_ACTA_MAT!="")){
        NRO_ACTA_PER = request.getParameter("NAPer").trim();
        NRO_ACTA_MAT = request.getParameter("NAMatr").trim();
        FECH_REG_MAT = request.getParameter("FRMatr").trim();
        IDUBIGEO_MAT = request.getParameter("UbMatr").trim();
        DNI_PER_1 = request.getParameter("DNIP1").trim();
        DNI_PER_2 = request.getParameter("DNIP2").trim();
        DNI_TEST_1 = request.getParameter("DNIT1").trim();
        DNI_TEST_2 = request.getParameter("DNIT2").trim();
        DNI_REGISTRADOR = request.getParameter("DNIReg").trim();
        COD_ESTADO = request.getParameter("CodEsta").trim();
        IDUBIGEO = request.getParameter("Ubig").trim();
        COD_NACIONALIDAD = request.getParameter("CodNac").trim();
        RUTA_ACTA_MAT = request.getParameter("RAMat").trim();
//        }
        
        String BTN = request.getParameter("btn1");
int RS=0;
        if (BTN.equals("guardarMatr")) {
            String Query = ("select * from TB_UBIGEO where idubigeo='" + IDUBIGEO + "'");
            conexionsqlserver con = new conexionsqlserver();
            Connection cn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(Query);
                if (rs.next()) {
                    RS=1;
                }
            } catch (Exception b) {
                b.printStackTrace();
            }
            if(RS==1){
            GuardarMatrimonio(request, response);
            }else{
                 request.setAttribute("msj", "1");
                getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
            }
                
        }
//        ajax*************
//        String ubigeo = request.getParameter("Ubig").trim();
//        if (BTN.equals("verificar")) {
//            String Query = ("select * from TB_UBIGEO where idubigeo='" + ubigeo + "'");
//            conexionsqlserver con = new conexionsqlserver();
//            Connection cn = null;
//            Statement st = null;
//            ResultSet rs = null;
//            try {
//                con.conectar();
//                cn = con.getconection();
//                st = cn.createStatement();
//                rs = st.executeQuery(Query);
//                if (rs.next()) {
//                    out.print("1");
//                } else {
//                    out.print("0");
//                }
//            } catch (Exception b) {
//                b.printStackTrace();
//            }
//
//        }
        //***********


    }

    protected void GuardarMatrimonio(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        LogicaMatrimonio matr = new LogicaMatrimonio();
        boolean IMat = matr.InsertarMatrimonio(NRO_ACTA_PER, NRO_ACTA_MAT, FECH_REG_MAT, IDUBIGEO_MAT, DNI_PER_1,
                DNI_PER_2, DNI_TEST_1, DNI_TEST_2, DNI_REGISTRADOR, COD_ESTADO, IDUBIGEO, COD_NACIONALIDAD, RUTA_ACTA_MAT);

        if (IMat) {
            request.setAttribute("msj", "2");
            getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
        } else {
            request.setAttribute("msj", "1");
            getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
